﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ByteSite.Quartz
{
    public class CustomJobListener : IJobListener
    {
        public string Name => "CustomJobListener";

        /// <summary>
        /// 停止执行
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default)
        {

            await Task.Run(() =>
            {

            });
        }
        /// <summary>
        /// 待执行
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default)
        {
            await Task.Run(() =>
            {

            });
        }
        /// <summary>
        /// 已执行
        /// </summary>
        /// <param name="context"></param>
        /// <param name="jobException"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default)
        {
            await Task.Run(() =>
            {

                Entitys.QuartzTask task = context.GetTaskOptions();

                IJobDetail jobDetail = context.JobDetail;
                JobKey jobKey = jobDetail.Key;
                string jobName = jobKey.Name;


                Entitys.QuartzListen log = new Entitys.QuartzListen();
                log.task_id = jobName;
                log.add_time = DateTime.Now;
                if (jobException == null)
                {
                    log.type = "info";
                    log.log = task.name + "(" + task.id + "):" + "执行完成";
                }
                else
                {
                    log.type = "error";
                    log.log = task.name + "(" + task.id + "):\r\n" + jobException.ToString();
                }

                new BLL.quartz_listen().Add(log);


            });
        }
    }
}
